﻿using System;
using System.Collections.Generic;
using System.Data;
using eTMS.Controllers.DangKyHocPhanService;
using eTMS.Controllers.SinhVienLopTinChiService;
using eTMS.Controllers.KhoaHocService;

namespace eTMS.Controllers
{
    public class DangKyHocPhanController : DangKyHocPhanControllerBase
    {
        public DangKyHocPhanController()
            : base()
        {
        }
        SinhVienLopTinChiService.SinhVienLopTinChiServiceClient wSinhVienLopTinChi;
        public void UpdateSinhVienLopTinChi(int LopTinChi_Id, int SinhVienLopTinChi_ID)
        {
            if (wSinhVienLopTinChi == null)
                wSinhVienLopTinChi = new SinhVienLopTinChiServiceClient();
            SinhVienLopTinChiEntity o = wSinhVienLopTinChi.SelectOne(SinhVienLopTinChi_ID);
            o.TrangThai = Commons.Constants.TRANGTHAI_DANGKYLOP_MOITAO;
            o.NgayDangKy = DateTime.Now;
            wSinhVienLopTinChi.Update(o);
        }
        public int AddNewSinhVienLopTinChi(int LopTinChi_ID, int SinhVien_ID)
        {
            if (wSinhVienLopTinChi == null)
                wSinhVienLopTinChi = new SinhVienLopTinChiServiceClient();
            SinhVienLopTinChiEntity o = new SinhVienLopTinChiEntity();
            o.LopTinChi_ID = LopTinChi_ID;
            o.SinhVien_ID = SinhVien_ID;
            o.TrangThai = Commons.Constants.TRANGTHAI_DANGKYLOP_MOITAO;
            o.NgayDangKy = DateTime.Now;
            return wSinhVienLopTinChi.SaveRet(o).SinhVienLopTinChi_ID;
        }
        public void LoadMonHocDaDangKy(int SinhVien_ID, int HocKy_ID, int NamHoc_ID, int ChuongTrinhDaoTao_ID)
        {
            Table = dao.SelectAllDSMonHocDaDangKy(SinhVien_ID, NamHoc_ID, HocKy_ID, ChuongTrinhDaoTao_ID).Tables[0];
            Table.Columns.Add("SoTien", typeof(decimal));
            foreach (DataRow row in Table.Rows)
            {
                row["SoTien"] = ConvertNumber(row["SoHocPhanHocPhi"]) * eTMS.Commons.Constants.HOCPHI_TINCHI;
            }
        }
        public void LoadLopHocPhanChoMonDaDangKy(int SinhVien_ID, int HocKy_ID, int NamHoc_ID)
        {
            if (wSinhVienLopTinChi == null)
                wSinhVienLopTinChi = new SinhVienLopTinChiServiceClient();
            DataTable tblLopTinChi = wSinhVienLopTinChi.SelectAllDSWithLopTinChiBySinhVien(SinhVien_ID, HocKy_ID, NamHoc_ID).Tables[0];
            if (!Table.Columns.Contains("MaLop"))
            {
                Table.Columns.Add("MaLop");
            }
            if (!Table.Columns.Contains("Lop_ID"))
            {
                Table.Columns.Add("Lop_ID");
            }
            if (!Table.Columns.Contains("SinhVienLopTinChi_ID"))
            {
                Table.Columns.Add("SinhVienLopTinChi_ID");
            }
            if (!Table.Columns.Contains("TrangThaiDangKy"))
            {
                Table.Columns.Add("TrangThaiDangKy");
            }
            foreach (DataRow row in Table.Rows)
            {
                DataRow[] rows = tblLopTinChi.Select("MonHoc_ID=" + row["MonHoc_ID"].ToString());
                if (rows.Length > 0)
                {
                    row["MaLop"] = rows[0]["MaLop"];
                    row["Lop_ID"] = rows[0]["LopTinChi_ID"];
                    row["SinhVienLopTinChi_ID"] = rows[0]["SinhVienLopTinChi_ID"];
                    row["TrangThaiDangKy"] = rows[0]["TrangThaiDangKy"];
                }
            }
        }
        public void RegisterMonHoc(List<int> LsMonHoc_ID, int HocKy_ID, int NamHoc_ID, int SinhVien_ID)
        {
            List<DangKyHocPhanEntity> ls = new List<DangKyHocPhanEntity>();
            foreach (int monHocId in LsMonHoc_ID)
            {
                DangKyHocPhanEntity o = new DangKyHocPhanEntity();
                o.HocKy_ID = HocKy_ID;
                o.NamHoc_ID = NamHoc_ID;
                o.MonHoc_ID = monHocId;
                o.NgayDangKy = DateTime.Now;
                o.SinhVien_ID = SinhVien_ID;
                ls.Add(o);
            }
            dao.SaveList(ls.ToArray());
        }
        public void RegisterMonHoc(int NamHoc_ID, int HocKy_ID, int KhoaHoc_ID)
        {
            NamHocService.NamHocEntity oNamHoc = NamHocController.FindNamHoc(NamHoc_ID);
            ChuongTrinhDaoTaoController ctrl = new ChuongTrinhDaoTaoController();
            DataTable tblSinhVien;
            SinhVienService.SinhVienServiceClient wSinhVien = new SinhVienService.SinhVienServiceClient();
            DangKyHocPhanService.DangKyHocPhanServiceClient wDangKyHP = new eTMS.Controllers.DangKyHocPhanService.DangKyHocPhanServiceClient();
            List<KhoaHocEntity> lsKhoaHoc = new List<KhoaHocEntity>();
            if (KhoaHoc_ID > 0)
            {
                lsKhoaHoc.Add(KhoaHocController.FindKhoaHoc(KhoaHoc_ID));
            }
            else
            {
                foreach (KhoaHocEntity oKhoaHoc in KhoaHocController.ListKhoaHocStatic)
                {
                    lsKhoaHoc.Add(oKhoaHoc);
                }
            }
            int currentCTDT = 0;
            foreach (NganhHocService.NganhHocEntity oNganhHoc in NganhHocController.ListNganhHocStatic)
            {
                foreach (KhoaHocEntity oKhoaHoc in lsKhoaHoc)
                {
                    ctrl.LoadChuongTrinhDaoTaoSuDungByKhoaNganh(oKhoaHoc.KhoaHoc_ID, oNganhHoc.NganhHoc_ID);
                    tblSinhVien = wSinhVien.SearchSinhVien_ID(-1, oNganhHoc.NganhHoc_ID, -1, oKhoaHoc.KhoaHoc_ID, null).Tables[0];
                    if (ctrl.ChuongTrinhDaoTaoObject == null) continue;
                    if (ctrl.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID != currentCTDT)
                    {
                        ctrl.LoadChiTiet(HocKy_ID, GetNamThu(oKhoaHoc.BatDau, oNamHoc.NgayBatDau));
                    }
                    List<int> lsSinhVien_ID = new List<int>();
                    foreach (DataRow r in tblSinhVien.Rows)
                    {
                        lsSinhVien_ID.Add(ConvertInt(r["SinhVien_ID"]));
                    }
                    if (lsSinhVien_ID.Count > 0)
                    {
                        foreach (DataRow rMonHoc in ctrl.TableChiTiet.Rows)
                        {
                            int MonHoc_ID = ConvertInt(rMonHoc["MonHoc_ID"]);
                            List<int> lsCurrentId = new List<int>(lsSinhVien_ID);
                            
                            DataTable tblExists = dao.SelectAllDSDangKyHocPhan_ID(NamHoc_ID, HocKy_ID, MonHoc_ID, lsSinhVien_ID.ToArray()).Tables[0];
                            foreach (DataRow rExists in tblExists.Rows)
                            {
                                lsCurrentId.Remove(ConvertInt(rExists["SinhVien_ID"]));
                            }
                            List<DangKyHocPhanEntity> lsDangKy = new List<DangKyHocPhanEntity>();
                            foreach (int svID in lsCurrentId)
                            {
                                DangKyHocPhanEntity o = new DangKyHocPhanEntity();
                                o.HocKy_ID = HocKy_ID;
                                o.MonHoc_ID = MonHoc_ID;
                                o.NamHoc_ID = NamHoc_ID;
                                o.NgayDangKy = DateTime.Now;
                                o.SinhVien_ID = svID;
                                lsDangKy.Add(o);
                            }
                            dao.SaveListDangKy(lsDangKy.ToArray());
                        }
                    }
                    currentCTDT = ctrl.ChuongTrinhDaoTaoObject.ChuongTrinhDaoTao_ID;
                }
            }
        }
    }
}
